/**
  ******************************************************************************
  * @file    sym32l010_hal_systick.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   SYSTICK HAL module driver.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_systick.h"


/******************************************************************************/
/* global variables                                                           */
/******************************************************************************/
__IO uint32_t       uwTick;
uint32_t            uwTickPrio = (1UL << __NVIC_PRIO_BITS);                     /* Invalid PRIO */
HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;                         /* 1KHz */


/*******************************************************************************
  * @brief  This function configures the source of the time base.
  *         The time source is configured  to have 1ms time base with a dedicated
  *         Tick interrupt priority.
  * @note   This function is called  automatically at the beginning of program after
  *         reset by HAL_Init() or at any time when clock is reconfigured  by HAL_SysCtrl_ClockConfig().
  * @note   In the default implementation, SysTick timer is the source of time base.
  *         It is used to generate interrupts at regular time intervals.
  *         Care must be taken if HAL_Delay() is called from a peripheral ISR process,
  *         The SysTick interrupt must have higher priority (numerically lower)
  *         than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  *         The function is declared as __Weak  to be overwritten  in case of other
  *         implementation  in user file.
  * @param  system hclk freq.
  * @retval HAL status
  */
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t HclkFreq)
{
    /*Configure the SysTick to have interrupt in 1ms time basis*/
    if (SysTick_Config(HclkFreq / (1000U / uwTickFreq)) > 0U)
    {
        return HAL_ERROR;
    }

    /* Configure the SysTick IRQ priority */
    if (TICK_INT_PRIORITY < (1UL << __NVIC_PRIO_BITS))
    {
        NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
        uwTickPrio = TICK_INT_PRIORITY;
    }
    else
    {
        return HAL_ERROR;
    }

    /* Return function status */
    return HAL_OK;
}


/*******************************************************************************
  * @brief  Provides a tick value in millisecond.
  * @note   This function is declared as __weak  to be overwritten  in case of other
  *         implementations in user file.
  * @retval tick value
  */
__weak uint32_t HAL_GetTick(void)
{
    return uwTick;
}


/*******************************************************************************
  * @brief  This function provides accurate delay (in milliseconds) based
  *         on variable incremented.
  * @note   In the default implementation, SysTick timer is the source of time base.
  *         It is used to generate interrupts at regular time intervals where uwTick
  *         is incremented.
  * @note   ThiS function is declared as __weak to be overwritten in case of other
  *         implementations in user file.
  * @param  Delay specifies the delay time length, in milliseconds.
  * @retval None
  */
__weak void HAL_Delay(uint32_t Delay)
{
    uint32_t tickstart = HAL_GetTick();
    uint32_t wait      = Delay;

    /* Add a freq to guarantee minimum wait */
    if (wait < HAL_MAX_DELAY)
    {
        wait += (uint32_t)(uwTickFreq);
    }

    while ((HAL_GetTick() - tickstart) < wait) { ; }
}


/*******************************************************************************
  * @brief  Suspend Tick increment.
  * @note   In the default implementation, SysTick timer is the source of time base. It is
  *         used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
  *         is called, the the SysTick interrupt will be disabled and so Tick increment
  *         is suspended.
  * @note   This function is declared as __weak to be overwritten in case of other
  *         implementations in user file.
  * @retval None
  */
__weak void HAL_SuspendTick(void)
{
    /* Disable SysTick Interrupt */
    REGBITS_CLR(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
}


/*******************************************************************************
  * @brief  Resume Tick increment.
  * @note   In the default implementation, SysTick timer is the source of time base. It is
  *         used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
  *         is called, the the SysTick interrupt will be enabled and so Tick increment
  *         is resumed.
  * @note   This function is declared as __weak  to be overwritten  in case of other
  *         implementations in user file.
  * @retval None
  */
__weak void HAL_ResumeTick(void)
{
    /* Enable SysTick Interrupt */
    REGBITS_SET(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
}


/*******************************************************************************
  * @brief  This function is called to increment  a global variable "uwTick"
  *         used as application time base.
  * @note   In the default implementation, this variable is incremented each 1ms
  *         in SysTick ISR.
  * @note   This function is declared as __weak to be overwritten in case of other
  *         implementations in user file.
  * @retval None
  */
__weak void HAL_IncTick(void)
{
    uwTick += uwTickFreq;
}


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
